
import BaseView from "../base/view/BaseView";
import PoolManager from "../pool/PoolManager";
import { isPoolUI } from "../tools/Define";

/**
 * auth cgw
 * 界面信息
 */
export default class LayerInfo {

    // 加载的预制体上挂载的view组件
    private layer: BaseView;

    //显示优先级 对于栈结构的管理器不起效果
    private priority: number = 0;

    private isPool:boolean = false;

    //黑色背景节点。根据view传进来的颜色创建，如果颜色值为null不创建。
    private parent: cc.Node

    constructor(layer: BaseView, priority: number = 0,isPool:boolean = false) {
        this.isPool = isPool
        this.layer = layer;
        this.priority = priority;
        // cc.log(" LayerInfo new ",layer);
        let bgColor = layer.getBgColor();
        if (bgColor) {
            this.addBlackBg(bgColor);
        }
    }

    getLayer(): BaseView {
        return this.layer;
    }

    private addBlackBg(bgColor: cc.Color) {
        // let prefab = ResLoader.instance().getRes(FWConfig.BLACK_PREFAB_NAME, cc.Prefab); 
        // if(prefab){
        //     //TODO  这里可以使用对象池优化 
        //     let bgView:cc.Node = cc.instantiate(prefab);
        //     if (bgColor) {
        //         let bg = bgView.getChildByName("bg");
        //         bg.color = cc.color(bgColor.getR(), bgColor.getG(), bgColor.getB());
        //         bg.opacity = bgColor.getA();
        //     }
        //     this.parent = bgView;
        //     this.parent.addChild(this.layer.node);
        // }
    }

    setVisible(flag) {
        this.getNode().active = flag;
    }

    // getLayer():BaseView{
    //     return this.layer;
    // }


    isHideBg() {
        return this.layer.isHideBg();
    }

    getNode(): cc.Node {
        if (this.parent) {
            return this.parent;
        } else {
            return this.layer.node;
        }
    }

    destroy() {
        if (this.isPool) {
            PoolManager.instance().put(this.getNode(), true)
        } else {
            this.getNode().destroy();
        }

    }

    getPriority(): number {
        return this.priority;
    }
}